package com.web.kongjian.ao.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.StringUtils;

import com.base.kongjian.entity.Comment;
import com.base.kongjian.entity.Reply;
import com.base.kongjian.entity.Weibo;
import com.base.kongjian.entity.WorkShareRec;
import com.base.kongjian.service.CommentService;
import com.base.kongjian.service.ReplyService;
import com.base.kongjian.service.WeiboService;
import com.base.kongjian.service.WorkShareRecService;
import com.web.kongjian.ao.WeiboAo;
import com.web.kongjian.ao.utils.WeiboUtils;
import com.web.user.member.utils.CookieUtils;

@Service("weiboAo")
public class WeiboAoImpl implements WeiboAo
{

	@Autowired
	private WeiboService weiboService;
	@Autowired
	private CommentService commentService;
	@Autowired
	private ReplyService replyService;
	@Autowired
	private  WorkShareRecService  workShareRecService;
	
	@Override
	public void send(Weibo weibo,HttpServletRequest request)
	{
		
		long memberId  = Long.parseLong (CookieUtils.getCookieValue (request ,"member_Id"));
		
		String content = weibo.getContent ();
		
		weibo.setTitle (WeiboUtils.getWeiboTitle (content));
		weibo.setPublishTime (System.currentTimeMillis ());
		weibo.setMemberId (memberId);
		
		weiboService.saveWeibo (weibo);
	}

	@Override
	public void delete(long id)
	{
		
		
	}

	@Override
	public void praise(long id ,long publisherId,HttpServletRequest request)
	{
		
		long memberId = Long.parseLong (CookieUtils.getCookieValue (request ,"member_id"));
		
		//1-ת�� ,2-�ղ�,3-����,4����
		weiboService.addPraiseCount  (id ,memberId);

		workShareRecService.addShareRec (id ,1 ,3 ,publisherId ,memberId);
	}

	@Override
	public void collect(long id, long publisherId,HttpServletRequest request)
	{
		long memberId = Long.parseLong (CookieUtils.getCookieValue (request ,"member_id"));
		
						//1-ת�� ,2-�ղ�,3-����,4����
		weiboService.addCollectionCount (id ,memberId);
	
		workShareRecService.addShareRec (id ,1 ,2 ,publisherId ,memberId);
	}

	@Override
	public String transmit(long id ,long publisherId,HttpServletRequest request)
	{
		String str = CookieUtils.getCookieValue (request ,"member_id");
		
		long sharerId = 0L;
		if(!StringUtils.isTrimEmpty (str))
		{
			sharerId = Long.parseLong (str);
		}
		
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("type" ,1);//ת��
		params.put ("sharerId" ,sharerId);
		params.put ("workId" ,id);
		
		WorkShareRec wsr = workShareRecService.getWSRByMap (params);
		
		if(ObjectUtils.notNull (wsr))
		{
			long currentTime = System.currentTimeMillis ();
			long publishTime = wsr.getShareTime ();
			if(currentTime-publishTime<600000)
			{
				
				return "ת����ʱ������������10����!";
			}else
			{
				wsr.setShareTime (System.currentTimeMillis ());
				workShareRecService.updateWSR(wsr);
				
				return null;
			}
		}
		
		//workShareRecService.addShareRec (id ,1 ,1 ,publisherId ,sharerId);
		weiboService.addTransmitCount (id ,sharerId);
		
		return null;
	}

	@Override
	public void comment(long memberId,long workId,String content,HttpServletRequest request)
	{
		String str = CookieUtils.getCookieValue (request ,"member_id");
		long id = 0L;
		if(!StringUtils.isTrimEmpty (str))
		{
			id = Long.parseLong (str);
		}
		Comment comment = new Comment ();
		comment.setMemberId (memberId);
		comment.setWorkId (workId);;
		comment.setComment_MId (id);
		comment.setContent (content);
		comment.setWorkKind (1);
		
		commentService.comment (comment);
		weiboService.addCommentCount (workId ,id);
	}

	@Override
	public void browse(long id, HttpServletRequest request)
	{
		
		
	}

	@Override
	public void sendReply(HttpServletRequest request, long commentId,long comment_MId, String content)
	{
		Reply reply = new Reply ();
		
		reply.setComment_MId (comment_MId);
		reply.setCommentId (commentId);
		reply.setContent (content);
		
		
		
		long reply_MId = Long.parseLong (CookieUtils.getCookieValue (request ,"member_id"));
		reply.setReply_MId (reply_MId);
		
		replyService.reply (reply);
		Comment comment = commentService.getCommentById (commentId);
		if(ObjectUtils.notNull (comment))
		{
			long workId = comment.getWorkId ();
			weiboService.addCommentCount (workId ,reply_MId);
		}
	}
}
